package com.ccjq.jq173.order;

import com.ccjq.jq173.member.po.User;
import com.ccjq.jq173.order.po.Order;
import com.ccjq.jq173.order.po.PageInfo;
import com.ccjq.jq173.order.service.OrderService;
import com.ccjq.jq173.order.service.impl.OrderServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OrderServlet extends HttpServlet {
    private OrderService service;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        String flag=request.getParameter("flag");
        String ps=request.getParameter("ps");
        if (StringUtils.isBlank(flag)){
            if(user!=null)
            {
                String userId = user.getUserId();
                String pagesize = request.getParameter("pageSize");//一页多少记录
                String pageno = request.getParameter("pageNo");//当前页
                if(pagesize==null || pagesize.equals("")) {
                    request.getSession().setAttribute("pagesize","6");
                    pagesize = "6";
                }
                if(pageno==null || pageno.equals("")) {
                    pageno = "1";       //起始页
                }



                service = new OrderServiceImpl();

                PageInfo<Order> po = service.queryOrders(userId,Integer.parseInt(pagesize),Integer.parseInt(pageno));
                request.setAttribute("po",po);
                request.getSession().setAttribute("pagesize",pagesize);
                request.getRequestDispatcher("/WEB-INF/jsp/order/order_list.jsp").forward(request,response);
                return;
            }
            else
            {
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            }
        }
        if (flag.equals("d")){
            String orderId=request.getParameter("orderId");
            Order order=service.getOrderDetail(orderId);
            request.setAttribute("order",order);
            request.getRequestDispatcher("/WEB-INF/jsp/order/order_detail.jsp").forward(request,response);
            return;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
